/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treeship.core.service.xml;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Moonchild
 */
public class XMLReader {

    public static DocumentWrapper getWrappedDocumentFromXMLRealFile(File file) {
        return new DocumentWrapper(parseXmlRealFile(file));
    }

    public static Document parseXmlResource(String filename) {
        boolean validating = false;
        try {
            // Create a builder factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);

            // Create the builder and parse the file
            File file = new File(factory.getClass().getResource("/xml/hierarchy.xml").toURI());
            Document doc = factory.newDocumentBuilder().parse(file);
            doc.getDocumentElement().normalize();
            return doc;
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        // A parsing error occurred; the xml input is not valid
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Deprecated
    public static Document parseXmlRealFile(File file) {
        boolean validating = false;
        try {
            // Create a builder factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);

            // Create the builder and parse the file
            Document doc = factory.newDocumentBuilder().parse(file);
            doc.getDocumentElement().normalize();
            return doc;
        } catch (SAXException e) {
            e.printStackTrace();
        // A parsing error occurred; the xml input is not valid
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Deprecated
    public static Document parseXmlFile(String filename) {
        boolean validating = false;
        try {
            // Create a builder factory
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validating);

            // Create the builder and parse the file
            Document doc = factory.newDocumentBuilder().parse(new File(filename));
            doc.getDocumentElement().normalize();
            return doc;
        } catch (SAXException e) {
            e.printStackTrace();
        // A parsing error occurred; the xml input is not valid
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static DocumentWrapper getWrappedDocumentFromXMLFile(String filename) {
        return new DocumentWrapper(parseXmlFile(filename));
    }

    public static DocumentWrapper getWrappedDocumentFromResource(String file) {
        return new DocumentWrapper(parseXmlResource(file));
    }

    @Deprecated
    public static List<Node> getListOfRealNodes(NodeList listProperty) {
        List<Node> li = new ArrayList<Node>();
        for (int i = 0; i < listProperty.getLength(); i++) {
            Node item = listProperty.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                li.add(item);
            }
        }
        return li;
    }

    protected static List<NodeWrapper> getListOfNodeWrappers(NodeList listProperty) {
        List<NodeWrapper> li = new ArrayList<NodeWrapper>();
        for (int i = 0; i < listProperty.getLength(); i++) {
            Node item = listProperty.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                li.add(new NodeWrapper(item));
            }
        }
        return li;
    }

    @Deprecated
    public static NodeList getNodeListChildrenOfStaticTag(Document doc, String tagname) {
        return doc.getElementsByTagName(tagname).item(0).getChildNodes();
    }

    private static String encodeAttributePrivate(String nomeAttribute, String valore) {
        return nomeAttribute + "=\"" + valore + "\" ";
    }

    public static String encodeAttribute(String nomeAttribute, double valore) {
        return encodeAttributePrivate(nomeAttribute, valore + "");
    }

    public static String encodeAttribute(String nomeAttribute, long valore) {
        return encodeAttributePrivate(nomeAttribute, valore + "");
    }

    public static String encodeAttribute(String nomeAttribute, String valore) {
        return encodeAttributePrivate(nomeAttribute, valore);
    }

    public static String openTagAndLeaveOpen(String nomeTag) {
        return "<" + nomeTag + " ";
    }

    public static String openTag(String nomeTag) {
        return "<" + nomeTag + ">";
    }

    public static String closeTag(String nomeTag) {
        return "</" + nomeTag + ">";
    }

    private static String encodeTagAndValuePrivate(String nomeTag, String value) {
        return "<" + nomeTag + ">" + value + "</" + nomeTag + ">";
    }

    public static String encodeTagAndValue(String nomeTag, long value) {
        return encodeTagAndValuePrivate(nomeTag, value + "");
    }

    public static String encodeTagAndValue(String nomeTag, String value) {
        return encodeTagAndValuePrivate(nomeTag, value);
    }
}
